package com.ctgu.chatapp.model.Dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy.REPLACE
import androidx.room.Query
import com.ctgu.chatapp.model.pojo.RecordNew
import com.ctgu.chatapp.showPojo.FriendMsg
import kotlinx.coroutines.flow.Flow

@Dao
interface MsgDao {
    @Query("select RecordNew.id,RecordNew.friendID,Information.profile,Information.nickName,RecordNew.context,date from RecordNew left join information on RecordNew.friendID = Information.chatID where selfID = :selfID order by date desc limit 1")
    fun getAllRecordByChatID(selfID: Long): Flow<List<FriendMsg>>

    @Insert
    suspend fun addRecord(record: RecordNew)

    @Insert(onConflict = REPLACE)
    suspend fun addAllRecord(list: List<RecordNew>)

    @Query("select * from RecordNew where selfID = :selfID and friendID =:friendID order by date desc limit 10")
    fun getChatRecord(selfID: Long, friendID: Long): List<RecordNew>

    @Query("select * from RecordNew where selfID = :selfID and friendID =:friendID and date <:timestamp order by date desc limit 20")
    suspend fun getOldRecord(selfID: Long, friendID: Long, timestamp: Long): List<RecordNew>

    @Query("delete from recordnew where id =:id")
    suspend fun deleteRecord(id: Long)
}